package com.hongkzh.www.look.enterprise.watch.view.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hongkzh.www.R;
import com.hongkzh.www.look.enterprise.watch.model.bean.AdvByIdBean;
import com.hongkzh.www.look.enterprise.watch.model.bean.AdvRecommendByAdvIdBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaPhotoBean;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaProductBean;
import com.hongkzh.www.look.enterprise.watch.view.customview.MyJZVideoPlayerStandard;
import com.hongkzh.www.other.banner.GlideImageLoader;
import com.hongkzh.www.look.enterprise.watch.view.customview.MyGSY;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;
import com.shuyu.gsyvideoplayer.utils.GSYVideoType;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZMediaManager;
import cn.jzvd.JZUserAction;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerManager;
import cn.jzvd.JZVideoPlayerStandard;

import static cn.jzvd.JZVideoPlayer.SCREEN_WINDOW_FULLSCREEN;

/**
 * Created by asad on 2018/3/27.
 * 看视频 adapter
 */

public class WatRvAdapter extends RecyclerView.Adapter implements View.OnClickListener, MyJZVideoPlayerStandard.Back {
    private final int HEAD = 0;
    private final int HEZUO = 1;
    private final int TUJI = 2;
    private final int ITEM = 0;
    private final int ITEM1 = 1;
    private final int ITEM2 = 2;
    private final int ITEM3 = 3;
    private final Activity activity;
    private List<String> title = new ArrayList<>();
    private List<String> wtjBan = new ArrayList<>();
    private AdvByIdBean advByIdBean = new AdvByIdBean();
    private GSYVideoHelper gsyVideoHelper;
    //    private MyGSY watchGsy;
    private MyJZVideoPlayerStandard videoPlayer;
    private WSPRvBQAdapter wspRvBQAdapter = new WSPRvBQAdapter();
    private WCPRvAdapter wcpRvAdapter = new WCPRvAdapter();
    private WSPRvTJAdapter wspRvTJAdapter = new WSPRvTJAdapter();

    public WatRvAdapter(Activity activity) {
        this.activity = activity;
        title.add("相关推荐");
        title.add("渠道合作");
        title.add("Ta的图集");
        title.add("Ta的产品");
    }

    @Override
    public int getItemViewType(int position) {
        if (position == HEAD)
            return ITEM;
        if (position == HEZUO)
            return ITEM1;
        if (position == TUJI)
            return ITEM2;
        return ITEM3;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == ITEM) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.watch_shipin, parent, false);
            return new ViewHolder(view);
        } else if (viewType == ITEM1) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.watch_hezuo, parent, false);
            return new ViewHolder1(view);
        } else if (viewType == ITEM2) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.watch_tuji, parent, false);
            return new ViewHolder2(view);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.watch_chanpin, parent, false);
            return new ViewHolder3(view);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case ITEM:
                final ViewHolder viewHolder = (ViewHolder) holder;
                viewHolder.wtTitle.setText(title.get(position));
                viewHolder.wtJt.setVisibility(View.GONE);
                String imgSrc = null;
                if (advByIdBean != null && advByIdBean.getData() != null) {
                    imgSrc = advByIdBean.getData().getImgSrc();
                    viewHolder.watchTitle.setText(advByIdBean.getData().getTitle());
                    viewHolder.bflTv.setText("" + advByIdBean.getData().getPlayCount());
                    viewHolder.xhlTv.setText("" + advByIdBean.getData().getPraiseCount());
                    viewHolder.fxlTv.setText("" + advByIdBean.getData().getShareCount());
                    Glide.with(viewHolder.itemView).load(advByIdBean.getData().getHeadImg()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(viewHolder.wjjIma);
                    viewHolder.wjjTitle.setText(advByIdBean.getData().getUName());
                    viewHolder.wjjGuanzhu.setText(advByIdBean.getData().getFollows() + "人已关注");
                }

//                watchGsy = viewHolder.watchGsy;
//                String source2 = "http://pc.v.k.360kan.com/vod-xinxiliu-tv-q2-bj/78262048_0-1514833924-85e9c022-bedb-5fb8.mp4?time=1523584824&sign=771a12b890578c66e27d433130bd790c&from=browser_from_1482144255";
//                String source2 = "http://video.7k.cn/app_video/20171202/6c8cf3ea/v.m3u8.mp4";
//                String source2 = "http://112.253.22.157/17/z/z/y/u/zzyuasjwufnqerzvyxgkuigrkcatxr/hc.yinyuetai.com/D046015255134077DDB3ACA0D7E68D45.flv";
                String source2 = "http://47.95.5.235:8081/file//userfiles/1/files/hkEnterpriseAdv/2018/04/asdf.avi";
//                watchGsy.setIsTouchWiget(false);
//                watchGsy.setIsTouchWigetFull(false);
//                GSYVideoHelper.GSYVideoHelperBuilder gsyVideoHelperBuilder = new GSYVideoHelper.GSYVideoHelperBuilder();
//                gsyVideoHelperBuilder
//                        .setHideActionBar(true)
//                        .setHideStatusBar(true)
//                        .setShowFullAnimation(true)
//                        .setLockLand(true);
//                gsyVideoHelper = new GSYVideoHelper(viewHolder.itemView.getContext(), watchGsy);
//                gsyVideoHelper.setGsyVideoOptionBuilder(gsyVideoHelperBuilder);
//                watchGsy.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        gsyVideoHelper.doFullBtnLogic();
//                    }
//                });
//                watchGsy.getBackButton().setVisibility(View.GONE);
//                if (imgSrc != null) watchGsy.setUp(imgSrc, false, null);
//                else watchGsy.setUp(source2, false, null);
                videoPlayer = viewHolder.videoPlayer;
                videoPlayer.setBack(this);
                if (imgSrc != null)
                    videoPlayer.setUp(imgSrc, JZVideoPlayerStandard.SCREEN_LAYOUT_NORMAL);
                else
                    videoPlayer.setUp(source2, JZVideoPlayerStandard.SCREEN_LAYOUT_NORMAL);

                viewHolder.wspRvBiaoqian.setLayoutManager(new LinearLayoutManager(viewHolder.itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
                viewHolder.wspRvBiaoqian.setAdapter(wspRvBQAdapter);

                viewHolder.wspRvTuijian.setLayoutManager(new LinearLayoutManager(viewHolder.itemView.getContext(), LinearLayoutManager.HORIZONTAL, false));
                viewHolder.wspRvTuijian.setAdapter(wspRvTJAdapter);
                break;
            case ITEM1:
                ViewHolder1 viewHolder1 = (ViewHolder1) holder;
                viewHolder1.wtTitle.setText(title.get(position));
                viewHolder1.wtBody.setVisibility(View.VISIBLE);
                break;
            case ITEM2:
                ViewHolder2 viewHolder2 = (ViewHolder2) holder;
                viewHolder2.wtTitle.setText(title.get(position));

                viewHolder2.wtjBan.setImageLoader(new GlideImageLoader())
                        .setImages(wtjBan)
                        .setIndicatorGravity(BannerConfig.RIGHT)
                        .start();
                break;
            case ITEM3:
                ViewHolder3 viewHolder3 = (ViewHolder3) holder;
                viewHolder3.wtTitle.setText(title.get(position));

                viewHolder3.wcpRv.setLayoutManager(new LinearLayoutManager(viewHolder3.itemView.getContext(), LinearLayoutManager.VERTICAL, false));
                viewHolder3.wcpRv.setAdapter(wcpRvAdapter);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onback() {

        activity.finish();
    }

    public boolean onBack() {
//        return gsyVideoHelper == null || gsyVideoHelper.backFromFull();
        return JZVideoPlayer.backPress();
    }

    public void onVideoPause() {
//        if (watchGsy != null) watchGsy.onVideoPause();
        if (videoPlayer != null) {
            videoPlayer.onEvent(JZUserAction.ON_CLICK_PAUSE);
            JZMediaManager.instance().mediaPlayer.pause();
            videoPlayer.onStatePause();
        }
    }

    public void onVideoResume() {
//        if (watchGsy != null) watchGsy.onVideoResume();
        if (videoPlayer != null) {
            videoPlayer.onEvent(JZUserAction.ON_CLICK_RESUME);
            JZMediaManager.instance().mediaPlayer.start();
            videoPlayer.onStatePlaying();
        }
    }

    public void addAdvById(AdvByIdBean advByIdBean) {
        this.advByIdBean = advByIdBean;
        wspRvBQAdapter.add(advByIdBean);
        wspRvBQAdapter.notifyDataSetChanged();
    }

    public void addAllAlbumByUserId(MediaPhotoBean mediaPhotoBean) {
        for (int i = 0; i < mediaPhotoBean.getData().getList().size(); i++) {
            wtjBan.add(mediaPhotoBean.getData().getList().get(i).getCoverPhoto());
        }
    }

    public void addAllProductByUserId(MediaProductBean mediaProductBean) {
        wcpRvAdapter.add(mediaProductBean);
        wcpRvAdapter.notifyDataSetChanged();
    }

    public void addAdvRecommendByAdvId(AdvRecommendByAdvIdBean advRecommendByAdvIdBean) {
        wspRvTJAdapter.add(advRecommendByAdvIdBean);
        wspRvTJAdapter.notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        //        @BindView(R.id.watch_gsy)
//        MyGSY watchGsy;
        @BindView(R.id.videoplayer)
        MyJZVideoPlayerStandard videoPlayer;
        @BindView(R.id.watch_title)
        TextView watchTitle;
        @BindView(R.id.bfl_tv)
        TextView bflTv;
        @BindView(R.id.xhl_tv)
        TextView xhlTv;
        @BindView(R.id.fxl_tv)
        TextView fxlTv;
        @BindView(R.id.wjj_ima)
        ImageView wjjIma;
        @BindView(R.id.wjj_title)
        TextView wjjTitle;
        @BindView(R.id.wjj_guanzhu)
        TextView wjjGuanzhu;
        @BindView(R.id.wsp_rv_biaoqian)
        RecyclerView wspRvBiaoqian;
        @BindView(R.id.wt_title)
        TextView wtTitle;
        @BindView(R.id.wt_body)
        TextView wtBody;
        @BindView(R.id.wt_jt)
        ImageView wtJt;
        @BindView(R.id.wsp_rv_tuijian)
        RecyclerView wspRvTuijian;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.wt_title)
        TextView wtTitle;
        @BindView(R.id.wt_body)
        TextView wtBody;
        @BindView(R.id.wt_jt)
        ImageView wtJt;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.wt_title)
        TextView wtTitle;
        @BindView(R.id.wt_body)
        TextView iwt1Body;
        @BindView(R.id.wtj_ban)
        Banner wtjBan;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder3 extends RecyclerView.ViewHolder {
        @BindView(R.id.wt_title)
        TextView wtTitle;
        @BindView(R.id.wcp_rv)
        RecyclerView wcpRv;

        ViewHolder3(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
